package com.lc.cehui.ui.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CompanySchoolListApi;
import com.lc.cehui.http.response.CompanySchoolListBean;
import com.lc.cehui.other.KeyboardWatcher;
import com.lc.cehui.ui.activity.WebVIewActivity;
import com.lc.cehui.widget.ClearEditText;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InformationActivity extends AppActivity implements KeyboardWatcher.SoftKeyboardStateListener,
        TextView.OnEditorActionListener{

    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.et_search)
    ClearEditText etSearch;
    @BindView(R.id.ll_seach)
    LinearLayout llSeach;
    @BindView(R.id.rv_searchRecords)
    RecyclerView rvSearchRecords;
    @BindView(R.id.ll_searchRecords)
    LinearLayout llSearchRecords;
    @BindView(R.id.rv_importantnews)
    RecyclerView rvImportantnews;
    @BindView(R.id.ll_search_content)
    LinearLayout llSearchContent;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    BaseQuickAdapter<CompanySchoolListBean.DataBean, BaseViewHolder> adapter;
    List<CompanySchoolListBean.DataBean> datelist=new ArrayList<>();
    private int mPage=1;
    private String flag;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        titleBar.setTitle("学校资讯");

        adapter=new BaseQuickAdapter<CompanySchoolListBean.DataBean, BaseViewHolder>(R.layout.item_fragment_3,datelist) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, CompanySchoolListBean.DataBean bean) {
                holder.setText(R.id.name_tv,bean.getName());
                holder.setText(R.id.tv_date,bean.getCreate_time());
                holder.setText(R.id.tv_content,bean.getTitle());
                holder.setText(R.id.tv_hits,"阅读量"+bean.getHits());

//                GlideApp.with(InformationActivity.this).load(bean.get()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) holder.getView(R.id.im_logo));
                RecyclerView rv_img=holder.getView(R.id.rv_img);
                rv_img.setLayoutManager(new GridLayoutManager(getContext(),3));
                BaseQuickAdapter imgadapter=new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_fragment_3_img,bean.getPicarr()) {

                    protected void convert(@NotNull BaseViewHolder holder, String o) {
                        GlideApp.with(getContext())
                                .load(o)
                                .apply(new RequestOptions().placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher))
                                .transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                        20, getContext().getResources().getDisplayMetrics())))
                                .into((ImageView) holder.getView(R.id.img));
                    }


                };
                rv_img.setAdapter(imgadapter);
            }
        };
        rvImportantnews.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                startActivity(new Intent(InformationActivity.this, WebVIewActivity.class).putExtra("info_id", datelist.get(position).getInfo_id() + "").putExtra("type_id",datelist.get(position).getType_id() + ""));
            }
        });
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                Log.e("TAG", "onRefresh:---- " );
                super.onRefresh(refreshLayout);
//                new Handler().postDelayed(() -> {

                mPage = 1;
                datelist.clear();
                company_school_list();
                refreshLayout.finishRefreshing();
//                }, 1000);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);



                if (flag.equals("T")) {
                    mPage++;
                    company_school_list();
                } else {

                }
                refreshLayout.finishLoadmore();

            }
        });
        showEmptyView(adapter);
        etSearch.setOnEditorActionListener(this);
    }


    @OnClick(R.id.tv_searchRecords)
    public void onClick() {
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_information;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        company_school_list();
    }

    private void company_school_list() {
        EasyHttp.post(this)
                .api(new CompanySchoolListApi().setPage(mPage).setType_id(getIntent().getStringExtra("id")).setKey(etSearch.getText().toString()))
                .request(new HttpCallback<CompanySchoolListBean>(this) {

                    @Override
                    public void onSucceed(CompanySchoolListBean data) {
                        flag = data.getFlag();
                        datelist.addAll(data.getData());
                       adapter.setNewData(datelist);
                       adapter.notifyDataSetChanged();

                    }
                });
    }

    @Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // 模拟点击登录按钮

            mPage = 1;
            datelist.clear();
            company_school_list();
            hideKeyboard(getCurrentFocus());
//            搜索


        }
        return  false;
    }

    @Override
    public void onSoftKeyboardOpened(int keyboardHeight) {

    }

    @Override
    public void onSoftKeyboardClosed() {

    }
}
